<template>
	<a-modal :title="title" :width="'65%'" :visible="visible" :maskClosable="false" destroyOnClose
		:confirmLoading="confirmLoading" @ok="handleSubmit" @cancel="handleCancel">
		<a-spin :spinning="formLoading">
			<a-form :form="form">
				<a-row>
					<a-col :span="12">
						<a-form-item label="不良池编码" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input disabled placeholder="空" v-decorator="['number']" />
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-form-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-input disabled placeholder="空" v-decorator="['orgIdSupplierName']" />
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-textarea :rows="4" :disabled="record.defectiveStatus != 1" placeholder="请输入备注" v-decorator="['remark']"
								style="width: 100%;"></a-textarea>
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
							<a-tag color="gray" v-if="record.defectiveStatus == 0">待审核</a-tag>
							<a-tag color="blue" v-if="record.defectiveStatus == 1">待处理</a-tag>
							<a-tag color="orange" v-if="record.defectiveStatus == 2">待补发</a-tag>
							<a-tag color="cyan" v-if="record.defectiveStatus == 3">已扣款</a-tag>
							<a-tag color="pink" v-if="record.defectiveStatus == 4">待入库</a-tag>
							<a-tag color="green" v-if="record.defectiveStatus == 5">已入库</a-tag>
							<a-tag color="red" v-if="record.defectiveStatus == 6">驳回待提交</a-tag>
							<a-tag color="#f50" v-if="record.defectiveStatus == 30">待扣款</a-tag>
							<a-tag color="#f50" v-if="record.defectiveStatus == 51">已入库（部分扣款）</a-tag>
						</a-form-item>
					</a-col>
					<a-col :span="24">
						<a-form-item :labelCol="labelColBlock" :wrapperCol="wrapperColBlock" label="选择寄回方式"
							v-show="record.defectiveStatus == 1 || record.defectiveStatus == 0 || record.defectiveStatus == 6">
							<a-radio-group style="width:100%"
								:disabled="((record.defectiveStatus == 0 || record.defectiveStatus == 6) && (dialogType == 'detail' || dialogType == 'examine'))"
								v-decorator="['returnMethod', { rules: [{ required: true, message: '请选择！' }] }]">
								<a-radio :value="1">寄回</a-radio>
								<a-radio :value="2">供应商带回</a-radio>
							</a-radio-group>
						</a-form-item>
					</a-col>
					<a-col :span="24">

						<a-form-item :labelCol="labelColBlock" :wrapperCol="wrapperColBlock" label="物流信息"
							v-show="form.getFieldValue('returnMethod') == 1">
							<a-input
								:disabled="record.defectiveStatus == 1 || record.defectiveStatus == 0 || record.defectiveStatus == 30 || (record.defectiveStatus == 6 && dialogType == 'detail')"
								style="width:100%" placeholder="请输入" v-decorator="['logisticsInfo']" />
						</a-form-item>
					</a-col>
					<a-col :span="24" v-if="record.sendBackTime">
						<a-form-item :labelCol="labelColBlock" :wrapperCol="wrapperColBlock" label="寄回时间"
							v-if="record.sendBackTime">
							<a-input disabled placeholder="空" v-decorator="['sendBackTime']" />
						</a-form-item>
					</a-col>
					<a-col :span="24">
						<a-form-item :labelCol="labelColBlock" :wrapperCol="wrapperColBlock" label="入库时间" v-if="record.rkTime">
							<a-input disabled placeholder="空" :value="record.rkTime" />
						</a-form-item>
					</a-col>
					<a-divider orientation="left" v-if="record.defectiveStatus == 0 && dialogType == 'examine'"> 审核信息 </a-divider>
					<a-col :span="24" v-if="record.defectiveStatus == 0 && dialogType == 'examine'">
						<a-form-item :labelCol="labelColBlock" :wrapperCol="wrapperColBlock" label="审核结果"
							v-if="record.defectiveStatus == 0">
							<a-radio-group style="width:100%"
								v-decorator="['auditStatus', { rules: [{ required: true, message: '请选择！' }] }]">
								<a-radio :value="2">通过</a-radio>
								<a-radio :value="3">驳回</a-radio>
							</a-radio-group>
						</a-form-item>
					</a-col>
					<a-col :span="24" v-if="record.defectiveStatus == 0 && dialogType == 'examine'">
						<a-form-item :labelCol="labelColBlock" :wrapperCol="wrapperColBlock" label="审核备注"
							v-if="record.defectiveStatus == 0">
							<div v-if="form.getFieldValue('auditStatus') == 3">
								<a-textarea placeholder="请输入"
									v-decorator="['auditRemark', { rules: [{ required: true, message: '请输入！' }] }]"
									:auto-size="{ minRows: 6 }" :maxLength="1000" />
							</div>
							<div v-else>
								<a-textarea placeholder="请输入"
									v-decorator="['auditRemark', { rules: [{ required: false, message: '请输入！' }] }]"
									:auto-size="{ minRows: 6 }" :maxLength="1000" />
							</div>

						</a-form-item>
					</a-col>
					<a-col :span="24" v-if="record.defectiveStatus == 30 && dialogType == 'deduction'">
						<a-form-item :labelCol="labelColBlock" :wrapperCol="wrapperColBlock" label="扣款时间">
							<a-date-picker v-decorator="['deductionTime', { rules: [{ required: true, message: '请选择！' }] }]"
								value-format="YYYY-MM-DD 00:00:00" />
						</a-form-item>
					</a-col>
				</a-row>
			</a-form>

			<div style="margin: 10px 5px;">
				<div v-if="[2, 4].includes(record.defectiveStatus)">
					<!-- <a-button type="danger" @click="handleReissue">补发</a-button> -->
					<a-button type="danger" @click="handleConfirmWarehousing">入库</a-button>
					<a-button type="danger" v-if="[4].includes(record.defectiveStatus) && record.materialType == '1473944069297156097'
						&& ['1502530819023044609', '1502530915877912578', '1871827435191992321'].includes(record.orgIdSupplier) && managerOrFinance()"
						@click="handleConfirmDeduction">扣款</a-button>
				</div>
				<div v-if="record.defectiveStatus == 1">
					<s-table ref="table" bordered style="margin-top: 15px;" size="default" :columns="columns" :data="loadData">
						<span slot="image" slot-scope="text,record">
							<image-preview1 :images="text" :imageUrl="record.emsMaterialResult.imageUrl" />
						</span>
					</s-table>
				</div>
				<div v-if="[2, 4].includes(record.defectiveStatus)">
					<s-table ref="table" bordered style="margin-top: 15px;" size="default" :scroll="{ x: 1000, y: 1000 }"
						:columns="columns1" :data="loadData" :rowKey="(record) => record.materialId"
						:rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }">
						<span slot="image" slot-scope="text,record">
							<image-preview1 :images="text" :imageUrl="record.emsMaterialResult.imageUrl" />
						</span>
						<span slot="status" slot-scope="text,record">
							<!-- <a-tag style="width:70px; text-align: center;" color="blue"
								v-if="record.reissueNum != record.num">待补发</a-tag> -->
							<a-tag style="width:70px; text-align: center;" color="green" v-if="record.rkNum == record.num">已入库</a-tag>
							<a-tag style="width:70px; text-align: center;" color="pink"
								v-else-if="record.reissueNum == record.num">待入库</a-tag>
						</span>
						<span slot="action" slot-scope="text,record">
							<!-- <a @click="$refs.reissueOrStoreRecord.init('reissueIn', record)">补发记录</a>
							<a-divider type="vertical" /> -->
							<a @click="$refs.reissueOrStoreRecord.init('storeIn', record)">入库记录</a>
						</span>
					</s-table>
				</div>
				<div v-if="[5].includes(record.defectiveStatus)">
					<s-table ref="table" bordered style="margin-top: 15px;" size="default" :scroll="{ x: 1000, y: 1000 }"
						:columns="columns1" :data="loadData">
						<span slot="image" slot-scope="text,record">
							<image-preview1 :images="text" :imageUrl="record.emsMaterialResult.imageUrl" />
						</span>
						<span slot="status" slot-scope="text,record">
							<!-- <a-tag style="width:70px; text-align: center;" color="blue"
								v-if="record.reissueNum != record.num">待补发</a-tag> -->
							<a-tag style="width:70px; text-align: center;" color="green" v-if="record.rkNum == record.num">已入库</a-tag>
							<a-tag style="width:70px; text-align: center;" color="orange"
								v-else-if="record.reissueNum == record.num">待入库</a-tag>
						</span>
						<span slot="action" slot-scope="text,record">
							<!-- <a @click="$refs.reissueOrStoreRecord.init('reissueIn', record)">补发记录</a>
							<a-divider type="vertical" /> -->
							<a @click="$refs.reissueOrStoreRecord.init('storeIn', record)">入库记录</a>
						</span>
					</s-table>
				</div>
				<div v-if="[3].includes(record.defectiveStatus)">
					<s-table ref="table" bordered style="margin-top: 15px;" size="default" :scroll="{ x: 1000, y: 1000 }"
						:columns="columns1" :data="loadData">
						<span slot="image" slot-scope="text,record">
							<image-preview1 :images="text" :imageUrl="record.emsMaterialResult.imageUrl" />
						</span>
						<span slot="status" slot-scope="text,record">
							<a-tag style="width:70px; text-align: center;" color="green">已扣款</a-tag>
						</span>
						<span slot="action" slot-scope="text,record">
							<!-- <a @click="$refs.reissueOrStoreRecord.init('reissueIn', record)">补发记录</a>
							<a-divider type="vertical" /> -->
							<a @click="$refs.reissueOrStoreRecord.init('storeIn', record)">入库记录</a>
						</span>
					</s-table>
				</div>
				<div v-if="[0].includes(record.defectiveStatus) && dialogType != 'detail'">
					<a-table ref="table" bordered style="margin-top: 15px;" size="default" :columns="columns0"
						:data-source="dataSource">
						<span slot="image" slot-scope="text,record">
							<image-preview1 :images="text" :imageUrl="record.emsMaterialResult.imageUrl" />
						</span>
						<span slot="status" slot-scope="text,record">
							<a-tag color="gray">待审核</a-tag>
						</span>
						<span slot="orderNumber" slot-scope="text,record">
							{{ record.orderNumber ? record.orderNumber : '--' }}
						</span>
						<span slot="num" slot-scope="text,record">
							{{ record.num ? record.num : '--' }}
						</span>
						<span slot="remark" slot-scope="text,record">
							{{ record.remark ? record.remark : '--' }}
						</span>
					</a-table>
				</div>
				<div v-if="[6].includes(record.defectiveStatus) && dialogType != 'detail'">
					<div>
						<a-button type="primary" @click="handldAdd">新增</a-button>
					</div>
					<a-table ref="table" bordered style="margin-top: 15px;" size="default" :columns="columns0"
						:data-source="dataSource">
						<span slot="image" slot-scope="text,record">
							<image-preview1 :images="text" :imageUrl="record.emsMaterialResult.imageUrl" />
						</span>
						<span slot="status" slot-scope="text,record">
							<a-tag style="width:70px; text-align: center;" color="gray">待审核</a-tag>
						</span>
						<span slot="orderNumber" slot-scope="text,record">
							<a-input placeholder="请输入采购单号" v-model="record.orderNumber" />
						</span>
						<span slot="num" slot-scope="text,record">
							<a-input-number placeholder="请输入不良数量" v-model="record.num" :min="1" />
						</span>
						<span slot="remark" slot-scope="text,record">
							<a-textarea placeholder="请输入备注" v-model="record.remark" auto-size />
						</span>
						<span slot="action" slot-scope="text,record,index">
							<a @click="handleRemove(record, index)">移除</a>
						</span>
					</a-table>
				</div>
				<div v-if="record.defectiveStatus == 0 && dialogType == 'detail'">
					<s-table ref="table" bordered style="margin-top: 15px;" size="default" :columns="columns0" :data="loadData">
						<span slot="image" slot-scope="text,record">
							<image-preview1 :images="text" :imageUrl="record.emsMaterialResult.imageUrl" />
						</span>
						<span slot="status" slot-scope="text,record">
							<a-tag color="gray">待审核</a-tag>
						</span>
						<span slot="orderNumber" slot-scope="text,record">
							{{ record.orderNumber ? record.orderNumber : '--' }}
						</span>
						<span slot="num" slot-scope="text,record">
							{{ record.num ? record.num : '--' }}
						</span>
						<span slot="remark" slot-scope="text,record">
							{{ record.remark ? record.remark : '--' }}
						</span>
					</s-table>
				</div>
				<div v-if="record.defectiveStatus == 6 && dialogType == 'detail'">
					<s-table ref="table" bordered style="margin-top: 15px;" size="default" :columns="columns0" :data="loadData">
						<span slot="image" slot-scope="text,record">
							<image-preview1 :images="text" :imageUrl="record.emsMaterialResult.imageUrl" />
						</span>
						<span slot="status" slot-scope="text,record">
							<a-tag color="red">驳回待提交</a-tag>
						</span>
						<span slot="orderNumber" slot-scope="text,record">
							{{ record.orderNumber ? record.orderNumber : '--' }}
						</span>
						<span slot="num" slot-scope="text,record">
							{{ record.num ? record.num : '--' }}
						</span>
						<span slot="remark" slot-scope="text,record">
							{{ record.remark ? record.remark : '--' }}
						</span>
					</s-table>
				</div>
				<div v-if="record.defectiveStatus == 51 && dialogType == 'detail'">
					<s-table ref="table" :scroll="{ x: 1400, y: 1000 }" bordered style="margin-top: 15px;" size="default"
						:columns="columns1" :data="loadData">
						<span slot="image" slot-scope="text,record">
							<image-preview1 :images="text" :imageUrl="record.emsMaterialResult.imageUrl" />
						</span>
						<span slot="status" slot-scope="text,record">
							<a-tag color="red">已入库（部分扣款）</a-tag>
						</span>
						<span slot="orderNumber" slot-scope="text,record">
							{{ record.orderNumber ? record.orderNumber : '--' }}
						</span>
						<span slot="num" slot-scope="text,record">
							{{ record.num ? record.num : '--' }}
						</span>
						<span slot="remark" slot-scope="text,record">
							{{ record.remark ? record.remark : '--' }}
						</span>
					</s-table>
				</div>
				<div v-if="record.defectiveStatus == 30 && dialogType == 'deduction'">
					<a-table ref="table" bordered style="margin-top: 15px;" size="default" :columns="columns_deduction"
						:data-source="dataSource">
						<span slot="image" slot-scope="text,record">
							<image-preview1 :images="text" :imageUrl="record.emsMaterialResult.imageUrl" />
						</span>
						<span slot="status" slot-scope="text,record">
							<a-tag color="#f50">待扣款</a-tag>
						</span>
						<span slot="orderNumber" slot-scope="text,record">
							{{ record.orderNumber ? record.orderNumber : '--' }}
						</span>
						<span slot="num" slot-scope="text,record">
							{{ record.num ? record.num : '--' }}
						</span>
						<span slot="remark" slot-scope="text,record">
							{{ record.remark ? record.remark : '--' }}
						</span>
						<span slot="deductionPrice" slot-scope="text,record">
							<a-input-number v-model="record.deductionPrice" placeholder="请输入" :precision="2" :min="0" />
						</span>
						<span slot="deductionNum" slot-scope="text,record">
							<a-input-number v-model="record.deductionNum" placeholder="请输入" :min="1" :max="record.num" />
						</span>
					</a-table>
				</div>
			</div>
			<reissueOrStoreForm ref="reissueOrStoreForm" @ok="handleRefresh" />
			<deductionForm ref="deductionForm" @ok="handleRefresh" />
			<reissueOrStoreRecord ref="reissueOrStoreRecord" />
			<selectMaterial ref="selectMaterialRef" @selected="selected" />
		</a-spin>
	</a-modal>
</template>

<script>
import {
	detail,
	getMaterialByDefectivePoolId,
	handleNew,
	caiwuAudit,//财务审核
	rejectHandle,//重新提交
	reissue,//补发
	confirmWarehousing,//入库
	deduction,//扣款
} from '@/api/modular/main/badPool'

import reissueOrStoreForm from './reissueOrStoreForm.vue';
import reissueOrStoreRecord from './reissueOrStoreRecord.vue';
import deductionForm from './deductionForm.vue';
import { STable } from '@/components'

import selectMaterial from './selectMaterial.vue'

export default {
	components: {
		STable,
		reissueOrStoreForm,
		reissueOrStoreRecord,
		selectMaterial,
		deductionForm
	},
	props: ['materialType'],
	data() {
		return {
			dialogType: '',
			fileList: [],
			record: {},
			selectedObj: {},
			labelCol: {
				xs: { span: 24 },
				sm: { span: 6 }
			},
			wrapperCol: {
				xs: { span: 24 },
				sm: { span: 15 }
			},
			labelColBlock: {
				xs: { span: 24 },
				sm: { span: 3 }
			},
			wrapperColBlock: {
				xs: { span: 24 },
				sm: { span: 18 }
			},
			orgTree: [],
			visible: false,
			title: "",
			confirmLoading: false,
			isPackaging: false,
			needDeleteFileUid: null,
			previewVisible: false,
			previewImage: '',
			formLoading: false,
			form: this.$form.createForm(this),
			orgTypes: [],
			columns: [
				{
					title: '序号',
					width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '料号',
					dataIndex: 'emsMaterialResult.materialCode'
				},
				{
					title: '名称',
					dataIndex: 'emsMaterialResult.materialName'
				},
				{
					title: '规格',
					dataIndex: 'emsMaterialResult.materialModelNumber'
				},
				{
					title: '采购价',
					width: '120px',
					dataIndex: 'emsMaterialResult.purchasePrice'
				},
				{
					title: '图片',
					align: 'center',
					dataIndex: 'emsMaterialResult.image',
					scopedSlots: { customRender: 'image' }
				},
				{
					title: '采购单号',
					dataIndex: 'orderNumber',
					align: 'center',
					customRender: (text, record, index) => record.orderNumber ? record.orderNumber : '-'
				},
				{
					title: '不良数量',
					dataIndex: 'num',
					align: 'center',
				},
				{
					title: '备注',
					dataIndex: 'remark',
				},
				{
					title: '扣款数量',
					width: '120px',
					dataIndex: 'deductionNum'
				},
				{
					title: '扣款金额',
					width: '120px',
					dataIndex: 'deductionPrice',
				},
				{
					title: '小计',
					width: '150px',
					customRender: (text, record, index) => `${(record.deductionNum || 0) * (record.deductionPrice || 0)}`
				},
			],
			columns1: [
				{
					title: '序号',
					width: '80px',
					align: 'center',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '料号',
					dataIndex: 'emsMaterialResult.materialCode',
					width: '220px',
				},
				{
					title: '名称',
					dataIndex: 'emsMaterialResult.materialName',
					width: '270px',
				},
				{
					title: '规格',
					width: '220px',
					dataIndex: 'emsMaterialResult.materialModelNumber'
				},
				{
					title: '采购价',
					width: '120px',
					dataIndex: 'emsMaterialResult.purchasePrice'
				},
				{
					title: '图片',
					dataIndex: 'emsMaterialResult.image',
					width: '80px',
					scopedSlots: { customRender: 'image' }
				},
				{
					title: '采购单号',
					dataIndex: 'orderNumber',
					width: '120px',
					customRender: (text, record, index) => record.orderNumber ? record.orderNumber : '-'
				},
				{
					title: '不良数量',
					dataIndex: 'num',
					width: '120px',
					align: 'center',
				},
				{
					title: '待入库数量',
					dataIndex: '',
					width: '120px',
					align: 'center',
					customRender: (text, record, index) => record.reissueNum - (record.rkNum ? record.rkNum : 0) - (record.deductionNum ? record.deductionNum : 0)
				},
				{
					title: '已入库数量',
					dataIndex: 'rkNum',
					width: '120px',
					align: 'center',
				},
				{
					title: '状态',
					dataIndex: 'status',
					width: '140px',
					align: 'center',
					scopedSlots: { customRender: 'status' }
				},
				{
					title: '备注',
					width: '120px',
					dataIndex: 'remark',
				},
				{
					title: '扣款数量',
					width: '120px',
					dataIndex: 'deductionNum'
				},
				{
					title: '扣款金额',
					width: '120px',
					dataIndex: 'deductionPrice',
				},
				{
					title: '小计',
					width: '150px',
					customRender: (text, record, index) => `${(record.deductionNum || 0) * (record.deductionPrice || 0)}`
				},
				{
					title: '操作',
					width: '220px',
					fixed: 'right',
					align: 'center',
					scopedSlots: { customRender: 'action' }
				},
			],
			//审核
			columns0: [
				{
					title: '序号',
					width: '80px',
					align: 'center',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '料号',
					dataIndex: 'emsMaterialResult.materialCode',
				},
				{
					title: '名称',
					dataIndex: 'emsMaterialResult.materialName',
				},
				{
					title: '规格',
					dataIndex: 'emsMaterialResult.materialModelNumber'
				},
				{
					title: '采购价',
					dataIndex: 'emsMaterialResult.purchasePrice'
				},
				{
					title: '图片',
					align: 'center',
					dataIndex: 'emsMaterialResult.image',
					scopedSlots: { customRender: 'image' }
				},
				{
					title: '状态',
					align: 'center',
					dataIndex: 'status',
					scopedSlots: { customRender: 'status' }
				},
				{
					title: '采购单号',
					dataIndex: 'orderNumber',
					scopedSlots: { customRender: 'orderNumber' },
				},
				{
					title: '不良数量',
					dataIndex: 'num',
					align: 'center',
					scopedSlots: { customRender: 'num' }
				},
				{
					title: '备注',
					scopedSlots: { customRender: 'remark' }
				},
				{
					title: '扣款数量',
					width: '120px',
					dataIndex: 'deductionNum'
				},
				{
					title: '扣款金额',
					width: '120px',
					dataIndex: 'deductionPrice',
				},
				{
					title: '小计',
					width: '150px',
					customRender: (text, record, index) => `${(record.deductionNum || 0) * (record.deductionPrice || 0)}`
				},
				{
					title: '操作',
					width: '150px',
					align: 'center',
					scopedSlots: { customRender: 'action' }
				},
			],
			//扣款
			columns_deduction: [
				{
					title: '序号',
					width: '80px',
					align: 'center',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '料号',
					dataIndex: 'emsMaterialResult.materialCode',
				},
				{
					title: '名称',
					dataIndex: 'emsMaterialResult.materialName',
				},
				{
					title: '规格',
					dataIndex: 'emsMaterialResult.materialModelNumber'
				},
				{
					title: '采购价',
					dataIndex: 'emsMaterialResult.purchasePrice'
				},
				{
					title: '图片',
					align: 'center',
					dataIndex: 'emsMaterialResult.image',
					scopedSlots: { customRender: 'image' }
				},
				{
					title: '状态',
					align: 'center',
					dataIndex: 'status',
					scopedSlots: { customRender: 'status' }
				},
				{
					title: '采购单号',
					dataIndex: 'orderNumber',
					scopedSlots: { customRender: 'orderNumber' },
				},
				{
					title: '不良数量',
					dataIndex: 'num',
					align: 'center',
					scopedSlots: { customRender: 'num' }
				},
				{
					title: '备注',
					scopedSlots: { customRender: 'remark' }
				},
				{
					title: '扣款数量',
					scopedSlots: { customRender: 'deductionNum' }
				},
				{
					title: '单个扣款金额',
					scopedSlots: { customRender: 'deductionPrice' }
				},
				{
					title: '小计',
					width: '150px',
					customRender: (text, record, index) => `${(record.deductionNum || 0) * (record.deductionPrice || 0)}`
				},
			],
			dataSource: [],
			queryParam: {},
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				let query = {
					...this.queryParam,
					...parameter,
				}
				return getMaterialByDefectivePoolId(query).then((res) => {
					return res.data
				})
			},
			selectedRowKeys: [],
			selectedRows: [],
		}
	},

	methods: {
		// 初始化方法
		init(dialogType, record) {
			this.record = { ...record }
			this.dialogType = dialogType
			this.visible = true;

			this.title = "详情";

			console.log("this.record--", this.record);
			this.computedColumns();
			if (record.defectiveStatus == 51) {
				this.columns1.splice(this.columns1.length - 1, 1);
			}
			this.queryParam.defectivePoolId = this.record.id;
			setTimeout(() => {
				this.form.setFieldsValue({
					...record
				})
				this.$refs.table.refresh()
			}, 500);
		},
		//扣款
		deduction(dialogType, record) {
			this.record = { ...record }
			this.dialogType = dialogType
			this.visible = true;
			this.title = "扣款";
			this.queryParam.defectivePoolId = this.record.id;
			setTimeout(() => {
				this.form.setFieldsValue({
					...record
				})
				let query = {
					...this.queryParam,
					pageSize: -1
				}
				getMaterialByDefectivePoolId(query).then((res) => {
					this.dataSource = res.data.rows.map(val => {
						return {
							...val,
							deductionNum: val.reissueNum - (val.rkNum ? val.rkNum : 0)
						}
					})
				})
			}, 500);
		},
		computedColumns() {
			if (this.managerOrFinance()) {
				this.columns = [
					{
						title: '序号',
						width: '80px',
						customRender: (text, record, index) => `${index + 1}`
					},
					{
						title: '料号',
						dataIndex: 'emsMaterialResult.materialCode'
					},
					{
						title: '名称',
						dataIndex: 'emsMaterialResult.materialName'
					},
					{
						title: '规格',
						dataIndex: 'emsMaterialResult.materialModelNumber'
					},
					{
						title: '采购价',
						width: '120px',
						dataIndex: 'emsMaterialResult.purchasePrice'
					},
					{
						title: '图片',
						align: 'center',
						dataIndex: 'emsMaterialResult.image',
						scopedSlots: { customRender: 'image' }
					},
					{
						title: '采购单号',
						dataIndex: 'orderNumber',
						align: 'center',
						customRender: (text, record, index) => record.orderNumber ? record.orderNumber : '-'
					},
					{
						title: '不良数量',
						dataIndex: 'num',
						align: 'center',
					},
					{
						title: '备注',
						dataIndex: 'remark',
					},
					{
						title: '扣款数量',
						width: '120px',
						dataIndex: 'deductionNum'
					},
					{
						title: '扣款金额',
						width: '120px',
						dataIndex: 'deductionPrice',
					},
					{
						title: '小计',
						width: '150px',
						customRender: (text, record, index) => `${(record.deductionNum || 0) * (record.deductionPrice || 0)}`
					},
				]
				this.columns1 = [
					{
						title: '序号',
						width: '80px',
						align: 'center',
						customRender: (text, record, index) => `${index + 1}`
					},
					{
						title: '料号',
						dataIndex: 'emsMaterialResult.materialCode',
						width: '220px',
					},
					{
						title: '名称',
						dataIndex: 'emsMaterialResult.materialName',
						width: '270px',
					},
					{
						title: '规格',
						width: '220px',
						dataIndex: 'emsMaterialResult.materialModelNumber'
					},
					{
						title: '采购价',
						width: '120px',
						dataIndex: 'emsMaterialResult.purchasePrice'
					},
					{
						title: '图片',
						dataIndex: 'emsMaterialResult.image',
						width: '80px',
						scopedSlots: { customRender: 'image' }
					},
					{
						title: '采购单号',
						dataIndex: 'orderNumber',
						width: '120px',
						customRender: (text, record, index) => record.orderNumber ? record.orderNumber : '-'
					},
					{
						title: '不良数量',
						dataIndex: 'num',
						width: '120px',
						align: 'center',
					},
					{
						title: '待入库数量',
						dataIndex: '',
						width: '120px',
						align: 'center',
						customRender: (text, record, index) => record.reissueNum - (record.rkNum ? record.rkNum : 0) - (record.deductionNum ? record.deductionNum : 0)
					},
					{
						title: '已入库数量',
						dataIndex: 'rkNum',
						width: '120px',
						align: 'center',
					},
					{
						title: '状态',
						dataIndex: 'status',
						width: '140px',
						align: 'center',
						scopedSlots: { customRender: 'status' }
					},
					{
						title: '备注',
						width: '120px',
						dataIndex: 'remark',
					},
					{
						title: '扣款数量',
						width: '120px',
						dataIndex: 'deductionNum'
					},
					{
						title: '扣款金额',
						width: '120px',
						dataIndex: 'deductionPrice',
					},
					{
						title: '小计',
						width: '150px',
						customRender: (text, record, index) => `${(record.deductionNum || 0) * (record.deductionPrice || 0)}`
					},
					{
						title: '操作',
						width: '220px',
						fixed: 'right',
						align: 'center',
						scopedSlots: { customRender: 'action' }
					},
				]
				//审核
				this.columns0 = [
					{
						title: '序号',
						width: '80px',
						align: 'center',
						customRender: (text, record, index) => `${index + 1}`
					},
					{
						title: '料号',
						dataIndex: 'emsMaterialResult.materialCode',
					},
					{
						title: '名称',
						dataIndex: 'emsMaterialResult.materialName',
					},
					{
						title: '规格',
						dataIndex: 'emsMaterialResult.materialModelNumber'
					},
					{
						title: '采购价',
						dataIndex: 'emsMaterialResult.purchasePrice'
					},
					{
						title: '图片',
						align: 'center',
						dataIndex: 'emsMaterialResult.image',
						scopedSlots: { customRender: 'image' }
					},
					{
						title: '状态',
						align: 'center',
						dataIndex: 'status',
						scopedSlots: { customRender: 'status' }
					},
					{
						title: '采购单号',
						dataIndex: 'orderNumber',
						scopedSlots: { customRender: 'orderNumber' },
					},
					{
						title: '不良数量',
						dataIndex: 'num',
						align: 'center',
						scopedSlots: { customRender: 'num' }
					},
					{
						title: '备注',
						scopedSlots: { customRender: 'remark' }
					},
					{
						title: '扣款数量',
						width: '120px',
						dataIndex: 'deductionNum'
					},
					{
						title: '扣款金额',
						width: '120px',
						dataIndex: 'deductionPrice',
					},
					{
						title: '小计',
						width: '150px',
						customRender: (text, record, index) => `${(record.deductionNum || 0) * (record.deductionPrice || 0)}`
					},
					{
						title: '操作',
						width: '150px',
						align: 'center',
						scopedSlots: { customRender: 'action' }
					},
				]
			} else {
				this.columns = [
					{
						title: '序号',
						width: '80px',
						customRender: (text, record, index) => `${index + 1}`
					},
					{
						title: '料号',
						dataIndex: 'emsMaterialResult.materialCode'
					},
					{
						title: '名称',
						dataIndex: 'emsMaterialResult.materialName'
					},
					{
						title: '规格',
						dataIndex: 'emsMaterialResult.materialModelNumber'
					},
					// {
					// 	title: '采购价',
					// 	width: '120px',
					// 	dataIndex: 'emsMaterialResult.purchasePrice'
					// },
					{
						title: '图片',
						align: 'center',
						dataIndex: 'emsMaterialResult.image',
						scopedSlots: { customRender: 'image' }
					},
					{
						title: '采购单号',
						dataIndex: 'orderNumber',
						align: 'center',
						customRender: (text, record, index) => record.orderNumber ? record.orderNumber : '-'
					},
					{
						title: '不良数量',
						dataIndex: 'num',
						align: 'center',
					},
					{
						title: '备注',
						dataIndex: 'remark',
					},
				]
				this.columns1 = [
					{
						title: '序号',
						width: '80px',
						align: 'center',
						customRender: (text, record, index) => `${index + 1}`
					},
					{
						title: '料号',
						dataIndex: 'emsMaterialResult.materialCode',
						width: '220px',
					},
					{
						title: '名称',
						dataIndex: 'emsMaterialResult.materialName',
						width: '270px',
					},
					{
						title: '规格',
						width: '220px',
						dataIndex: 'emsMaterialResult.materialModelNumber'
					},
					{
						title: '图片',
						dataIndex: 'emsMaterialResult.image',
						width: '80px',
						scopedSlots: { customRender: 'image' }
					},
					{
						title: '采购单号',
						dataIndex: 'orderNumber',
						width: '120px',
						customRender: (text, record, index) => record.orderNumber ? record.orderNumber : '-'
					},
					{
						title: '不良数量',
						dataIndex: 'num',
						width: '120px',
						align: 'center',
					},
					{
						title: '待入库数量',
						dataIndex: '',
						width: '120px',
						align: 'center',
						customRender: (text, record, index) => record.reissueNum - (record.rkNum ? record.rkNum : 0) - (record.deductionNum ? record.deductionNum : 0)
					},
					{
						title: '已入库数量',
						dataIndex: 'rkNum',
						width: '120px',
						align: 'center',
					},
					{
						title: '状态',
						dataIndex: 'status',
						width: '140px',
						align: 'center',
						scopedSlots: { customRender: 'status' }
					},
					{
						title: '备注',
						width: '120px',
						dataIndex: 'remark',
					},
					{
						title: '操作',
						width: '220px',
						fixed: 'right',
						align: 'center',
						scopedSlots: { customRender: 'action' }
					},
				]
				//审核
				this.columns0 = [
					{
						title: '序号',
						width: '80px',
						align: 'center',
						customRender: (text, record, index) => `${index + 1}`
					},
					{
						title: '料号',
						dataIndex: 'emsMaterialResult.materialCode',
					},
					{
						title: '名称',
						dataIndex: 'emsMaterialResult.materialName',
					},
					{
						title: '规格',
						dataIndex: 'emsMaterialResult.materialModelNumber'
					},
					// {
					// 	title: '采购价',
					// 	dataIndex: 'emsMaterialResult.purchasePrice'
					// },
					{
						title: '图片',
						align: 'center',
						dataIndex: 'emsMaterialResult.image',
						scopedSlots: { customRender: 'image' }
					},
					{
						title: '状态',
						align: 'center',
						dataIndex: 'status',
						scopedSlots: { customRender: 'status' }
					},
					{
						title: '采购单号',
						dataIndex: 'orderNumber',
						scopedSlots: { customRender: 'orderNumber' },
					},
					{
						title: '不良数量',
						dataIndex: 'num',
						align: 'center',
						scopedSlots: { customRender: 'num' }
					},
					{
						title: '备注',
						scopedSlots: { customRender: 'remark' }
					},
					{
						title: '操作',
						width: '150px',
						align: 'center',
						scopedSlots: { customRender: 'action' }
					},
				]
			}
		},
		//财务审核
		examine(dialogType, record) {
			this.record = { ...record }
			this.dialogType = dialogType
			this.visible = true;
			this.title = "审核";
			console.log("this.record--", this.record);
			this.queryParam.defectivePoolId = this.record.id;
			setTimeout(() => {
				this.form.setFieldsValue({
					...record
				})
				let query = {
					...this.queryParam,
					pageSize: -1
				}

				this.columns0.splice(this.columns0.length - 1, 1);
				getMaterialByDefectivePoolId(query).then((res) => {
					console.log(res, '------------------------');

					this.dataSource = res.data.rows
				})
			}, 500);
		},
		//重新提交
		reSubmit(dialogType, record) {
			this.record = { ...record }
			this.dialogType = dialogType
			this.visible = true;
			this.title = "重新提交";
			console.log("this.record--", this.record);
			this.queryParam.defectivePoolId = this.record.id;
			setTimeout(() => {
				this.form.setFieldsValue({
					...record
				})
				let query = {
					...this.queryParam,
					pageSize: -1
				}
				getMaterialByDefectivePoolId(query).then((res) => {
					console.log(res, '------------------------');

					this.dataSource = res.data.rows
				})
			}, 500);
		},
		selected(data) {
			console.log(data);
			let newData = JSON.parse(JSON.stringify(data));
			newData.num = null;
			newData.status = 0;
			newData.orderNumber = '';
			newData.remark = '';
			let emsMaterialResult = {
				materialCode: data.materialCode,
				materialName: data.materialName,
				materialModelNumber: data.materialModelNumber,
				purchasePrice: data.purchasePrice,
				image: data.image,
				imageUrl: data.imageUrl,
			}
			newData.emsMaterialResult = emsMaterialResult;
			this.dataSource.push(newData);
		},
		handleRemove(record, index) {
			this.dataSource.splice(index, 1);
		},
		handldAdd() {
			console.log(this.record);
			let selectedMaterial = this.dataSource.map(val => {
				return val.materialId
			})
			this.$refs.selectMaterialRef.init({
				materialType: this.record.materialType,
				orgIdSupplier: this.record.orgIdSupplier,
				selectedMaterial: selectedMaterial,
			})
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},

		//补发
		handleReissue() {
			if (this.selectedRows.length == 0) {
				this.$message.error("请先选择需要补发的物料");
				return
			}
			let list = this.selectedRows.filter(val => val.num - (val.reissueNum ? val.reissueNum : 0) != 0);
			console.log(list);
			if (list.length > 0) {
				this.$refs.reissueOrStoreForm.init('reissue', list)
			} else {
				this.$message.error("所选物料中，没有需要补发的物料");
			}
		},

		//入库
		handleConfirmWarehousing() {
			if (this.selectedRows.length == 0) {
				this.$message.error("请先选择需要入库的物料");
				return
			}
			let list = this.selectedRows.filter(val => val.reissueNum - (val.rkNum ? val.rkNum : 0) != 0);
			console.log(list);
			if (list.length > 0) {
				this.$refs.reissueOrStoreForm.init('store', list)
			} else {
				this.$message.error("所选物料中，没有需要入库的物料");
			}
		},
		//扣款
		handleConfirmDeduction() {
			if (this.selectedRows.length == 0) {
				return this.$message.error("请先选择需要扣款的物料");
			}
			this.$refs.deductionForm.init(this.record, this.selectedRows)
		},

		getDetail() {
			detail({
				id: this.record.id
			}).then(res => {
				console.log(res.data);

				setTimeout(() => {
					this.form.setFieldsValue({
						...res.data
					})
					this.record = res.data;
					this.$refs.table.refresh()
				}, 500);
			})
		},
		handleRefresh() {
			this.getDetail();
			this.selectedRowKeys = [];
			this.selectedRows = [];
			// this.$refs.table.refresh();//刷新关联物料
		},
		handleSubmit(e) {
			const { form: { validateFields } } = this

			validateFields((errors, values) => {
				console.log(errors);
				if (!errors) {
					//待处理
					if (this.record.defectiveStatus == 1) {
						let params = {
							id: this.record.id,
							remark: values.remark,
							returnMethod: values.returnMethod,
							logisticsInfo: values.logisticsInfo
						}
						this.confirmLoading = true
						console.log(params);
						handleNew(params).then(res => {
							this.$message.success("操作成功");
							this.$emit("ok");
							this.handleCancel();
						}).finally((res) => {
							this.confirmLoading = false
						})
					} else if (this.record.defectiveStatus == 0 && this.dialogType == 'examine') {
						let params = {
							id: this.record.id,
							auditStatus: values.auditStatus,
							auditRemark: values.auditRemark,
						}
						this.confirmLoading = true
						caiwuAudit(params).then(res => {
							if (res.code == 200) {
								this.$message.success("审核成功");
								this.$emit("ok");
								this.handleCancel();
							}
						}).finally((res) => {
							this.confirmLoading = false
						})
					} else if (this.record.defectiveStatus == 6 && this.dialogType == 'reSubmit') {
						if (this.dataSource.length == 0) {
							this.$message.error("物料不能为空");
							return
						}
						let list = this.dataSource.filter(val => val.num == null || !val.num);
						console.log(list);

						if (list.length > 0) {
							this.$message.error("不良物料数量为必填");
							return
						}
						let defectivePoolMaterialList = this.dataSource.map(val => {
							return {
								id: val.id ? val.id : "",
								remark: val.remark,
								materialId: val.materialId,
								orderNumber: val.orderNumber,
								num: val.num
							}
						})
						let params = {
							id: this.record.id,
							remark: values.remark,
							returnMethod: values.returnMethod,
							logisticsInfo: values.logisticsInfo,
							defectivePoolMaterialList: defectivePoolMaterialList
						}
						console.log("params----------", params);
						this.confirmLoading = true;
						rejectHandle(params).then(res => {
							if (res.code == 200) {
								this.$message.success("审核成功");
								this.$emit("ok");
								this.handleCancel();
							}
						}).finally((res) => {
							this.confirmLoading = false
						})
					} else if (this.record.defectiveStatus == 30 && this.dialogType == 'deduction') {
						let index = this.dataSource.filter(val => !val.deductionNum || !val.deductionPrice);
						if (index.length != 0) {
							return this.$message.error("扣款数量/扣款金额不能为空");
						}
						let deductionList = this.dataSource.map(val => {
							return {
								defectivePoolMaterialId: val.id,
								deductionNum: val.deductionNum,
								deductionPrice: val.deductionPrice
							}
						})
						let params = {
							id: this.record.id,
							deductionTime: values.deductionTime,
							deductionList: deductionList
						}
						this.confirmLoading = true;
						deduction(params).then(res => {
							if (res.code == 200) {
								this.$message.success("扣款成功");
								this.$emit("ok");
								this.handleCancel();
							}
						}).finally((res) => {
							this.confirmLoading = false
						})
					} else {
						this.handleCancel();
					}
				} else {
					this.confirmLoading = false
				}
			})
		},
		handleCancel() {
			this.form.resetFields();
			this.selectedRowKeys = [];
			this.selectedRows = [];
			this.visible = false
			this.$emit("ok");
		}
	}
}
</script>

<style scoped>
.inputDiv {
	width: 100%;
	line-height: 32px;
	cursor: pointer;
	background: #eee;
	border: 1px solid #bbb;
	padding-left: 10px;
}

::v-deep .ant-form-item-control {
	min-width: 250px;
}
</style>
